﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using CSharpDesign.DAL;

namespace CSharpDesign.BLL
{
    public class EditOrderService
    {
        SqlHelper sqloperate = new SqlHelper();
        SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        public DataRowView getMyDrv(string orderId)
        {
            DataRowView mydrvname = GetMyDrvName(orderId);

            string sqlstr = "select sum(shuliang) as TNum,sum(SPPrice*shuliang) as TPrice from vb_OrderInfo where OrderID='"
                + Convert.ToString(mydrvname.Row["OrderID"]) + "'";
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
            DataSet myds = new DataSet();
            myda.Fill(myds, "vb_OrderInfo");
            DataRowView mydrv = myds.Tables["vb_OrderInfo"].DefaultView[0];
            return mydrv;
        }

        public DataRowView GetMyDrvminfo(string orderId, string txtOMember)
        {
            string sqlstrminfo = "select * from Users where UserName='" + txtOMember + "'";

            SqlDataAdapter mydaminfo = new SqlDataAdapter(sqlstrminfo, sqlconn);
            DataSet mydsminfo = new DataSet();
            mydaminfo.Fill(mydsminfo, "Users");
            DataRowView mydrvminfo = mydsminfo.Tables["Users"].DefaultView[0];
            return mydrvminfo;
        }

        public DataRowView GetMDrvBind(GridView gvPOInfo, string orderId)
        {
            DataRowView mydrvname = GetMyDrvName(orderId);

            string sqlstr = "select sum(shuliang) as TNum,sum(SPPrice*shuliang) as TPrice from vb_OrderInfo where OrderID='"
                + Convert.ToString(mydrvname.Row["OrderID"]) + "'";
            string sqlstrpart = "select * from vb_OrderInfo where OrderID='"
                                + orderId + "'";
            SqlDataAdapter mydabind = new SqlDataAdapter(sqlstrpart, sqlconn);
            DataSet mydsbind = new DataSet();
            mydabind.Fill(mydsbind, "vb_OrderInfo");

            gvDataBind(gvPOInfo, sqlstrpart);
            return mydsbind.Tables["vb_OrderInfo"].DefaultView[0]; 
        }

        public DataRowView GetMyDrvName(string orderId)
        {
            string strname = "select OrderID,OrderMember from vb_OrderInfo where OrderID='" + orderId + "'";
            SqlDataAdapter mydaname = new SqlDataAdapter(strname, sqlconn);
            DataSet mydsname = new DataSet();
            mydaname.Fill(mydsname, "vb_OrderInfo");
            DataRowView mydrvname = mydsname.Tables["vb_OrderInfo"].DefaultView[0];
            return mydrvname;
        }


        public void gvDataBind(GridView gv, string sqlstr)
        {
            sqlconn.Open();
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
            DataSet myds = new DataSet();
            myda.Fill(myds);
            gv.DataSource = myds;
            gv.DataBind();
            sqlconn.Close();
        }
    }
}